package com.wb.oldwomen;

import android.app.AlertDialog;

import cn.cmgame.billing.api.GameInterface;

import com.wb.fw.GateActivity;
import com.wb.fw.Global;
import com.wb.fw.IConstants;
import com.wb.fw.SoundManager;
import com.wb.fw.Util;
import com.wiyun.engine.nodes.Button;
import com.wiyun.engine.nodes.ColorLayer;
import com.wiyun.engine.nodes.Sprite;

public class UIMainMenu extends ColorLayer implements IConstants {
	public static UIMainMenu make(GameScene parent) {
		return new UIMainMenu(parent);
	}
	Sprite mbg;
	Button mbtnSOn,mbtnSOff;
	GameScene mparent;
	protected UIMainMenu(GameScene parent) {
		mparent = parent;
		setKeyEnabled(true);
		setTouchEnabled(true);
		
		mbg = Sprite.make(R.drawable.m_bg);
		mbg.autoRelease();
		mbg.setPosition(Global.sWYSize.width/2, Global.sWYSize.height/2);
		mbg.setScale(Global.sScaleRate);
		addChild(mbg);
		
		Button btnStart = Button.make(R.drawable.play_n, R.drawable.play_p, R.drawable.play_n, R.drawable.play_n, this, "xlPlay");
		btnStart.autoRelease();
		btnStart.setPosition(Global.sStartX + Util.getScaleX(355), Global.sStartY - Util.getScaleY(325));
		btnStart.setScale(Global.sScaleRate);
		addChild(btnStart);
		
		float posx = Global.sStartX + Util.getScaleX(720);
		float posy_start = Global.sStartY - Util.getScaleY(60);
		float mheight = Util.getScaleY(90);
		
		Button btnHelp = Button.make(R.drawable.help_n, R.drawable.help_p, R.drawable.help_n, R.drawable.help_n, this, "xlHelp");
		btnHelp.autoRelease();
		btnHelp.setPosition(posx, posy_start);
		btnHelp.setScale(Global.sScaleRate*0.95f);
		addChild(btnHelp);
		
		posy_start -= mheight;
		mbtnSOn = Button.make(R.drawable.soundon_n, R.drawable.soundon_p, R.drawable.soundon_n, R.drawable.soundon_n, this, "xlCloseSound");
		mbtnSOn.autoRelease();
		mbtnSOn.setPosition(posx, posy_start);
		mbtnSOn.setScale(Global.sScaleRate*0.95f);
		addChild(mbtnSOn);
		
		mbtnSOff = Button.make(R.drawable.soundoff_n, R.drawable.soundoff_p, R.drawable.soundoff_n, R.drawable.soundoff_n, this, "xlOpenSound");
		mbtnSOff.autoRelease();
		mbtnSOff.setPosition(posx, posy_start);
		mbtnSOff.setScale(Global.sScaleRate*0.95f);
		addChild(mbtnSOff);
		
		checkSoundState();
		
		posy_start -= mheight;
		Button btnAbout = Button.make(R.drawable.about_n, R.drawable.about_p, R.drawable.about_n, R.drawable.about_n, this, "xlAbout");
		btnAbout.autoRelease();
		btnAbout.setPosition(posx, posy_start);
		btnAbout.setScale(Global.sScaleRate);
		addChild(btnAbout);
		
		Button btnMGame = Button.make(R.drawable.moregame_n, R.drawable.moregame_p, R.drawable.moregame_n, R.drawable.moregame_n, this, "xlMGame");
		btnMGame.autoRelease();
		btnMGame.setAnchor(1, 0);
		btnMGame.setPosition(Global.sEndX, Global.sEndY);
		btnMGame.setScale(Global.sScaleRate);
		addChild(btnMGame);
		
		SoundManager.playMusic(R.raw.menu);
	}
	
	public void tick() {
		
	}

	/**
	 * 进入主菜单的时候，需要确定声音开关ui。
	 */
	public void checkSoundState() {
		mbtnSOn.setVisible(false);
		mbtnSOff.setVisible(false);
		(SoundManager.isSoundOpen()?mbtnSOn:mbtnSOff).setVisible(true);
	}

	/**
	 * 开始
	 */
	public void xlPlay() {
		SoundManager.clickMenuItem();
		if (Util.isFirst(GateActivity.sInstance)) {
			mparent.switchState(GS_GAME_HELP);
		} else {
			mparent.switchState(GS_GAME_LEVEL_SELECT);
		}
	}
	/**
	 * 更多游戏
	 */
	public void xlMGame() {
		SoundManager.clickMenuItem();
		GameInterface.viewMoreGames(GateActivity.sInstance);
	}
	public void xlHelp() {
		SoundManager.clickMenuItem();
		mparent.switchState(GS_GAME_HELP);
	}


	/**
	 * 关于
	 */
	public void xlAbout() {
		SoundManager.clickMenuItem();
		GateActivity.sInstance.runOnUiThread(new Runnable() {
			@Override
			public void run() {
				new AlertDialog.Builder(GateActivity.sInstance)
				.setTitle("关于")
				.setMessage(R.string.app_about)
				.setNegativeButton("返回", null)
				.create()
				.show();
			}
		});
	}

	/**
	 * 退出
	 */
	public void xlExit() {
		SoundManager.clickMenuItem();
		mparent.ExitAppConfirm();
	}

	/**
	 * 关闭声音
	 */
	public void xlCloseSound() {
		SoundManager.clickMenuItem();
		mbtnSOn.setVisible(false);
		mbtnSOff.setVisible(true);
		SoundManager.enable(false);
	}

	/**
	 * 打开声音
	 */
	public void xlOpenSound() {
		mbtnSOn.setVisible(true);
		mbtnSOff.setVisible(false);
		SoundManager.enable(true);
		SoundManager.resumeMusic();
	}

	@Override
	protected boolean onBackButton() {
		//返回键退出。
		if (isVisible()) {
			mparent.ExitAppConfirm();
			return true;
		} else {
			return super.onBackButton();
		}
	}
}
